﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using AppSettingsLib;

namespace SharedSettingsDemo_App2
{
	public partial class Form1 : Form
	{
		DemoSettingsManager m_settings = new DemoSettingsManager();

		//--------------------------------------------------------------------------------
		public Form1()
		{
			InitializeComponent();
		}

		//--------------------------------------------------------------------------------
		private void Form1_Load(object sender, EventArgs e)
		{
			UpdateControls();
		}

		//--------------------------------------------------------------------------------
		private void buttonDone_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		//--------------------------------------------------------------------------------
		private void buttonResetToDefault_Click(object sender, EventArgs e)
		{
			// reset the user settings 
			m_settings.UserSettings.Reset();
			// update the controls
			UpdateControls();
		}

		//--------------------------------------------------------------------------------
		private void buttonSetAsdefaults_Click(object sender, EventArgs e)
		{
			int age;
			if (Int32.TryParse(textboxAge.Text, out age))
			{
				// put the data into user settings
				m_settings.UserSettings.Name	= textboxName.Text;
				m_settings.UserSettings.Age		= age;
				m_settings.UserSettings.Old		= checkboxOld.Checked;
				// put the data into default settings
				m_settings.DefaultSettings.Name	= textboxName.Text;
				m_settings.DefaultSettings.Age	= age;
				m_settings.DefaultSettings.Old	= checkboxOld.Checked;
				m_settings.DefaultSettings.Save();
				// update the controls
				UpdateControls();
			}
			else
			{
				MessageBox.Show("The age nmust be a valid integer.");
			}
		}

		//--------------------------------------------------------------------------------
		private void buttonLoadUser_Click(object sender, EventArgs e)
		{
			// load the settings
			m_settings.UserSettings.Load();
			// update the controls
			UpdateControls();
		}

		//--------------------------------------------------------------------------------
		private void buttonSaveUser_Click(object sender, EventArgs e)
		{
			int age;
			if (Int32.TryParse(textboxAge.Text, out age))
			{
				// put the data into the settings
				m_settings.UserSettings.Name	= textboxName.Text;
				m_settings.UserSettings.Age	= age;
				m_settings.UserSettings.Old	= checkboxOld.Checked;
				// save the data
				m_settings.UserSettings.Save();
			}
			else
			{
				MessageBox.Show("The age nmust be a valid integer.");
			}
		}

		//--------------------------------------------------------------------------------
		/// <summary>
		/// Updates the controls after the settings have been loaded or reset.
		/// </summary>
		private void UpdateControls()
		{
			// get the current default settings
			this.labelDefaultName.Text		= m_settings.DefaultSettings.Name;
			this.labelDefaultAge.Text		= m_settings.DefaultSettings.Age.ToString();
			this.checkboxDefaultOld.Checked	= m_settings.DefaultSettings.Old;
			// get the current user settings
			this.textboxName.Text			= m_settings.UserSettings.Name;
			this.textboxAge.Text			= m_settings.UserSettings.Age.ToString();
			this.checkboxOld.Checked		= m_settings.UserSettings.Old;
		}
	}
}
